package br.com.empresa.controller;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import br.com.empresa.app.Principal;
import br.com.empresa.dao.AlunoDao;
import br.com.empresa.dao.ProfessorDao;
import br.com.empresa.modelo.Aluno;
import br.com.empresa.modelo.Professor;
import br.com.empresa.modelo.TabelaPessoaFisica;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

public class TelaPrincipalController implements Initializable {
	
	 @FXML
	    private TableView<TabelaPessoaFisica> tabelaLista;
	 @FXML
	 private TableColumn clRg;
	 @FXML
	 private TableColumn clNome;
     @FXML
	 private TableColumn clTipo;
     @FXML
	 private TableColumn clCpf;
     @FXML
	 private TableColumn clTelefone;
	 @FXML
	 private TableColumn clCodigo;

	    @FXML
	    private Button btAluno;

	    @FXML
	    private Button btExcluir;

	    @FXML
	    private Button btPesquisar;

	

	    @FXML
	    private Button btNotas;

	    @FXML
	    private TextField txProcurar;

	    

	    @FXML
	    private Button btProfessor;

	    @FXML
	    void eventInserirAluno(ActionEvent event) {
	    	try{
	    		Parent root = FXMLLoader.load(getClass().getResource("/br/com/empresa/view/aluno.fxml"));
	    		Principal.SCENE.setRoot(root);
	    	} catch(IOException ex){
	    		ex.printStackTrace();
	    	}
	    	
	    }

	    @FXML
	    void eventExcluir(ActionEvent event) {

	    }

	    @FXML
	    void eventPesquisar(ActionEvent event) {

	    }

	    @FXML
	    void eventProfessor(ActionEvent event) {

	    }

	    @FXML
	    void eventNotas(ActionEvent event) {

	    }
	    
		@Override
		public void initialize(URL arg0, ResourceBundle arg1) {
			carregarTabela();
			
		}

		private void carregarTabela() {
			ObservableList<TabelaPessoaFisica> dados = FXCollections.observableArrayList();
			AlunoDao dao = new AlunoDao();
			List<Aluno> aluno = dao.listar();
			
			for(Aluno al: aluno){
				dados.add(new TabelaPessoaFisica(al));
			}
			
			ProfessorDao daop = new ProfessorDao();
			List<Professor> professor = daop.listar();
			for(Professor prof: professor){
				dados.add(new TabelaPessoaFisica(prof));
			}
			
			clCodigo.setCellValueFactory(new PropertyValueFactory("idcodigo"));
			clNome.setCellValueFactory(new PropertyValueFactory("nome"));
			clTelefone.setCellValueFactory(new PropertyValueFactory("telefone"));
			clCpf.setCellValueFactory(new PropertyValueFactory("cpf"));
			clRg.setCellValueFactory(new PropertyValueFactory("rg"));
			tabelaLista.setItems(dados);
			
		}
		
		
		

}
